/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entities;

import constants.Consts;
import java.io.Serializable;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author Bari
 */
@Entity
@Table(name = "resourcecategories")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Resourcecategory.findAll", query = "SELECT r FROM Resourcecategory r"),
    @NamedQuery(name = "Resourcecategory.findById", query = "SELECT r FROM Resourcecategory r WHERE r.id = :id"),
    @NamedQuery(name = "Resourcecategory.findByName", query = "SELECT r FROM Resourcecategory r WHERE r.name = :name")})
public class Resourcecategory extends AbstractJsonMaterial implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Size(max = 255)
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "categoryid")
    private Collection<Resource> resourcesCollection;

    public Resourcecategory() {
    }

    public Resourcecategory(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public Collection<Resource> getResourcesCollection() {
        return resourcesCollection;
    }

    public void setResourcesCollection(Collection<Resource> resourcesCollection) {
        this.resourcesCollection = resourcesCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Resourcecategory)) {
            return false;
        }
        Resourcecategory other = (Resourcecategory) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entities.Resourcecategory[ id=" + id + " ]";
    }

    @Override
    public JSONObject toJson() {
        try {
            JSONObject json=new JSONObject();
            json.put(Consts.RCATEGORY_NAME, name);
            json.put(Consts.RCATEGORY_ID, id);
            return json;
        } catch (JSONException ex) {
            Logger.getLogger(Resourcecategory.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
}
